import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class Image{
    public String url;
}
@WebServlet("/imageWall")
public class ImageWall extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        List<Image> list = fun();
        TemplateEngine engine = (TemplateEngine) this.getServletContext().getAttribute("engine");
        WebContext webContext = new WebContext(req,resp,this.getServletContext());
        webContext.setVariable("images",list);
        String html = engine.process("imageWall",webContext);
        System.out.println(html);
        resp.getWriter().write(html);
    }

    private List<Image> fun() {
        List<Image> list = new ArrayList<>();
        ServletContext context = this.getServletContext();
        String path = context.getRealPath("/images");
        File file = new File(path);
        File[] files = file.listFiles();
        for (File file1: files) {
            Image image = new Image();
            image.url = "images/" + file1.getName();
            list.add(image);
        }
        return list;
    }
}
